
import libuv.uv ;

import 
	std.algorithm,
	std.array,
	std.conv,
	std.stdio;

import
	tango.io.device.Array,
	tango.stdc.stringz,
	tango.io.Stdout ,
	
	tango.math.random.Random,
	tango.net.device.Berkeley,
	tango.net.InternetAddress,
	tango.net.InternetAddress,
	Int	= tango.text.convert.Integer ,
	tango.core.Array ,
	tango.io.device.Array ,
	tango.io.device.File ;
	
alias File	= tango.io.device.File.File ;

extern(C){
	struct uv_struct_field {
		char*	name ;
		int		size, off ;
	}
	struct uv_struct_info {
		char*	name ;
		int		size, field_count ;
		uv_struct_field*	list ;
	}
	struct uv_struct_list {
		int struct_count ; 
		uv_struct_info* list;
	}
	uv_struct_list* get_uv_struct_info();
}

void main() {
	auto list 	= get_uv_struct_info();
	
	auto _ip	= list.list ;
	
	auto bu	= new Array(1024, 1024) ;
	
	bu("module uvstructinfo.data ;\n");
	bu("import uvstructinfo.type ;\n");
	bu("import libuv.uv ;\n");
	
	bu("\nuv_struct_list getuvstructinfo(){\n");
	
	bu("\tuv_struct_list list;\n");
	
	for( int i = 0; i < list.struct_count; i++ ) {
		auto it	= _ip[i];
		auto name	= to!string( it.name );
		auto _ptr	= it.list ;
		
		bu("\tlist")  (" ~= new uv_struct_info(")
			("\"")(name)("\", ")
			( Int.toString(it.size) )(", ")
			( name )(".sizeof ")
		(");\n");
		
	
		for( int j = 0; j < it.field_count ; j++ ) {
			auto ptr	= _ptr[j] ;
			auto field	= to!string( ptr.name );
			bu("\tlist[")(  Int.toString(i) )("]") (".fields")   (" ~= new uv_struct_field(")
				("\"")(field)("\", ")
				("typeof(")(name)(".init.")(field)(").stringof , ")
				( Int.toString(ptr.size) )(",")
				( Int.toString(ptr.off) )(",")
				( name )(".init.")(field)(".sizeof , ")
				( name )(".init.")(field)(".offsetof, ")
			(");\n");
			
		}
	}
	bu("\treturn list;\n");
	bu("}\n");
	
	auto data = cast(string) bu.slice ;
	
	writefln("%s", data);
	File.set(`./export/data.d`, data);
}
